<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.appid"
        placeholder="appid"
        style="width: 180px;"
        class="filter-item"
        clearable
      />
      <el-input
        v-model="listQuery.appName"
        placeholder="应用名称"
        clearable
        style="width: 180px;"
        class="filter-item"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <div style="display:inline-block;">
        <router-link :to="{path:`/sys/app/add`}">
          <el-button
            v-if="isP('SysAppAdd')"
            class="filter-item light-primary-btn"
            style="margin-left: 10px;"
            type="primary"
            icon="el-icon-plus"
          >
            新增
          </el-button>
        </router-link>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        prop="appid"
        label="appid"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.appid }}</span>
          <i class="el-icon-share" title="查看AppSecrect" @click="viewAppsecrect(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column
        prop="appName"
        label="应用名称"
        width="180"
      />
      <el-table-column
        prop="ip"
        label="ip范围"
        width="180"
      />
      <el-table-column
        prop="contacts"
        label="联系人"
        width="100"
      />
      <el-table-column
        prop="mobile"
        label="联系方式"
        width="120"
      />
      <el-table-column
        prop="validDate"
        label="有效日期"
        width="100"
      />
      <el-table-column label="授权类型" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.authType===2" style="color: green">appsecrect</span>
          <span v-else style="color: blue">appid</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="是否禁用" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.status===0" style="color: green">正常</span>
          <span v-else style="color: red">禁用</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="240">
        <template slot-scope="scope">
          <el-button
            v-if="isP('SysAppGet')"
            style="margin-left: 10px"
            type="success"
            size="mini"
            @click="handleInfo(scope)"
          >详细
          </el-button>
          <el-button
            v-if="isP('SysAppUpdate')"
            style="margin-left: 10px"
            type="primary"
            size="mini"
            @click="handleUpdate(scope)"
          >修改
          </el-button>
          <el-button
            v-if="isP('SysAppDelete')"
            style="margin-left: 10px"
            type="danger"
            size="mini"
            @click="handleDelete(scope)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="listSysApp"
    />
  </div>
</template>

<script>
import { listSysApp, deleteSysApp, viewAppsecrect } from '@/api/sys/app'
import Pagination from '@/components/Pagination'
import msgUtil from '@/utils/msgUtil'

export default {
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: JSON.parse(localStorage.getItem('SysAppQuery')) || {
        appid: '',
        appName: '',
        page: 1,
        limit: 20
      }
    }
  },
  created() {
    this.listSysApp()
  },
  methods: {
    listSysApp() {
      this.listLoading = true
      listSysApp(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      })
      localStorage.setItem('SysAppQuery', JSON.stringify(this.listQuery))
    },
    // 按钮查询
    handleFilter() {
      this.listQuery.page = 1
      this.listSysApp()
    },
    // 新增
    handleAdd() {
      // this.$router.push({ path: '/app/add' }) // 带参跳转
    },
    // 删除
    handleDelete({ $index, row }) {
      msgUtil.confirm('确认删除应用?').then(async() => {
        await deleteSysApp({ appid: row.appid })
        msgUtil.success2('删除成功!')
        this.listSysApp()
      })
    },
    // 处理修改
    handleUpdate({ $index, row }) {
      this.$router.push({
        path: '/sys/app/update',
        query: { appid: row.appid }
      })
    },
    // 查看详情
    handleInfo({ $index, row }) {
      this.$router.push({
        path: '/sys/app/info',
        query: { appid: row.appid }
      })
    },
    // 查看secrect
    viewAppsecrect(row) {
      viewAppsecrect({ 'appid': row.appid }).then(response => {
        this.$alert(response.data, '查看AppSecrect', {
          confirmButtonText: '确定',
          center: true
        })
      })
    }
  }
}
</script>
